<?php

class NewsletterContentController extends AController {

    public function loadModel($id) {
        $model = DNewsletterContent::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function actions() {
        return array(
            'sortOrder' => array(
                'class'        => 'ext.mylib.widgets.OrderColumn.OrderAction',
                'modelClass'   => 'DNewsletterContent',
            ),
            'toggleActive' => array(
                'class'      => 'ext.mylib.widgets.ActiveColumn.ActiveAction',
                'modelClass' => 'DNewsletterContent',
            ),
        );
    }

    public function init() {
        parent::init();
        SpsState::setPrefix('DNewsletterContent');
    }

    public function actionIndex() {
        if (Yii::app()->request->isAjaxRequest) {
            SpsState::writeState('DNewsletterContent');
        }

        $model = new DNewsletterContent('search');
        $model->unsetAttributes();  // clear any default values

        if (isset($_GET['DNewsletterContent']))
            $model->attributes = $_GET['DNewsletterContent'];
        elseif (SpsState::hasState('search'))
            $model->attributes = SpsState::getState('search');

        $this->render('index', array(
            'model' => $model,
        ));
    }

    public function actionCreate() {
        $model = new DNewsletterContent;

        $this->performAjaxValidation($model);

        if (isset($_POST['DNewsletterContent'])) {
            $model->attributes = $_POST['DNewsletterContent'];

            if ($model->save()) {
                Yii::app()->user->setFlash('message', 'created');
                $model->clearSession();
                $this->redirect(array('index'));
            }
        } else {
            if (Yii::app()->user->hasState('news_folder_id')) {
                $model->folder_id = Yii::app()->user->getState('news_folder_id');
            } else {
                $model->folder_id = uniqid();
                Yii::app()->user->setState('news_folder_id', $model->folder_id);
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    public function actionUpdate($id) {
        Yii::app()->user->setFlash('keepState', true);

        $model = $this->loadModel($id);
        // $this->performAjaxValidation($model);

        if (isset($_POST['DNewsletterContent'])) {
            $model->attributes = $_POST['DNewsletterContent'];
            if ($model->save()) {
                Yii::app()->user->setFlash('message', 'updated');
                $model->clearSession();
                $this->redirect(array('index'));
            }
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        if ( Yii::app()->request->isAjaxRequest ) {
            if ( ! in_array($id, Yii::app()->params['id_reserved']['newsletter_content']) ) {
                $this->loadModel($id)->delete();
                $result['success'] = true;
                $result['message'] = Yii::t('DadminModule.msg', 'deleted');
            } else {
                $result['success'] = false;
                $result['message'] = Yii::t('DadminModule.msg', 'newsletter_content_deny_delete');
            }
            
            echo json_encode($result);
        } else {
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'newsletter-content-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

    /**
     * AJAX SUPPROTING FOR ASSIGNING LINK
     */
    public function actionAssignLink($type_id='') {
        $this->layout = '//layouts/admin/main_none';
        $model        = new DNewsletterContent('getCategory');
        $model->unsetAttributes();  // clear any default values
        
        if (isset($_GET['DNewsletterContent']))
            $model->attributes = $_GET['DNewsletterContent'];

        $this->render('_assign_link', array(
            'model' => $model,
            'data'  => $model->getCategory($type_id),
        ));
    }

    public function actionClearSession($scenario='') {
        $this->detectAjaxRequest();
        $model = new DNewsletterContent();
        $model->clearSession($scenario);
        echo 'success';
    }
    
    private function detectAjaxRequest() {
        if (! Yii::app()->request->isAjaxRequest) {
            throw new CHttpException('403', 'Forbidden');
            Yii::app()->end();
        }
    }

}